package com.dx.as.upload;

import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by jwaoo on 2017/7/17.
 */

public class DownloadFile {

    public DownloadFile(){
    }

    public void doDownLoad(final int msgId,final String url,final String filePath ,final DownloadInfo downloadInfo){
//        return new Thread(new Runnable() {
//            @Override
//            public void run() {
                DownloadManager.addDownload(msgId,downloadInfo);
                OkHttpClient client = new OkHttpClient();
                final Request request = new Request.Builder().url(url).build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        downloadInfo.onDownLoadFailed();
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        InputStream inputStream = null;
                        byte[] buf = new byte[2048];
                        int len = 0;
                        FileOutputStream outputStream = null;
                        try{
                            inputStream = response.body().byteStream();
                            long total = response.body().contentLength();
                            File file = new File(filePath);
                            outputStream = new FileOutputStream(file);
                            long sum = 0;
                            while((len = inputStream.read(buf)) != -1){
                                outputStream.write(buf,0,len);
                                sum += len;
                                DownloadManager.getDownloadObject(msgId).onDownProgress(sum,total);
                                downloadInfo.onDownProgress(sum,total);
                            }
                            outputStream.flush();
                            downloadInfo.onDownLoadSuccess();
                        }catch (Exception ex){
                            ex.printStackTrace();
                            downloadInfo.onDownLoadFailed();
                        }finally {

                            try {
                                if (inputStream != null) inputStream.close();
                            }catch (Exception ex){
                                ex.printStackTrace();
                            }
                            try{
                                if (outputStream != null) outputStream.close();
                            }catch (Exception ex){
                                ex.printStackTrace();
                            }
                        }
                    }
                });
//            }
//        });

    }

}
